<template>
  <div class="min-h-screen bg-gray-100 py-8 px-4">
    <div class="max-w-md mx-auto bg-white shadow-lg rounded-lg p-6">
      <h1 class="text-2xl font-bold text-center mb-6">数据库切换</h1>

      <!-- 数据库下拉选择 -->
      <div class="mb-4">
        <label for="database-select" class="block text-sm font-medium text-gray-700 mb-2">选择数据库</label>
        <select id="database-select"
                v-model="selectedDatabase"
                @change="switchDatabase"
                class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500">
          <option v-for="db in databases" :key="db" :value="db">{{ db }}</option>
        </select>
      </div>

      <!-- 当前数据库显示 -->
      <div v-if="selectedDatabase" class="mt-4 p-3 bg-blue-50 text-blue-800 rounded-md text-sm">
        当前数据库：{{ selectedDatabase }}
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="flex justify-center items-center mt-6">
        <div class="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-blue-500"></div>
      </div>

      <!-- 错误提示 -->
      <div v-if="error" class="mt-4 p-3 bg-red-50 text-red-800 rounded-md text-sm">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';

const databases = ref<string[]>([]);
const selectedDatabase = ref<string>('');
const loading = ref(false);
const error = ref<string | null>(null);

// 获取数据库列表
async function fetchDatabases() {
  try {
    const response = await fetch('http://localhost:8080/db/list');
    if (!response.ok) {
      throw new Error('获取数据库失败');
    }
    const data = await response.json();
    databases.value = data as string[];
  } catch (err) {
    if (err instanceof Error) {
      error.value = err.message;
    } else {
      error.value = '获取数据库失败';
    }
  }
}

// 切换数据库
async function switchDatabase() {
  const dbName = selectedDatabase.value;
  if (!dbName) return;

  loading.value = true;
  error.value = null;

  try {
    const response = await fetch('http://localhost:8080/db/switch', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: new URLSearchParams({ dbName })
    });

    if (!response.ok) {
      throw new Error('切换数据库失败');
    }

    alert(`已切换到数据库: ${dbName}`);
  } catch (err) {
    if (err instanceof Error) {
      error.value = err.message;
    } else {
      error.value = '切换数据库失败';
    }
    selectedDatabase.value = ''; // 回退选择
  } finally {
    loading.value = false;
  }
}

onMounted(() => {
  fetchDatabases();
});
</script>


<style scoped>
/* 页面背景和布局 */
.min-h-screen {
  min-height: 100vh;
  background: linear-gradient(135deg, #e0f7fa, #e8eaf6);
  padding: 2rem 1rem;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

/* 白色卡片 */
.max-w-md {
  max-width: 28rem;
  background-color: #fff;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border-radius: 1rem;
  padding: 2rem;
  margin-top: 40px;
  transition: transform 0.2s ease;
}

.max-w-md:hover {
  transform: translateY(-3px);
}

/* 标题 */
h1 {
  font-size: 1.5rem;
  font-weight: 700;
  text-align: center;
  color: #333;
  margin-bottom: 1.5rem;
}

/* label */
label {
  font-size: 0.875rem;
  font-weight: 500;
  color: #555;
  margin-bottom: 0.5rem;
  display: block;
}

/* 下拉框 */
select {
  width: 100%;
  padding: 0.5rem 1rem;
  border: 1px solid #ccc;
  border-radius: 0.375rem;
  background-color: #f9f9f9;
  color: #444;
  transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

select:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

/* 当前数据库提示 */
.bg-blue-50 {
  background-color: #eff6ff;
  color: #1e40af;
  border-left: 4px solid #3b82f6;
  padding: 0.75rem;
  border-radius: 0.375rem;
  margin-top: 1rem;
  font-size: 0.875rem;
}

/* 加载动画 */
.animate-spin {
  border-top-color: #42a5f5;
  border-bottom-color: #42a5f5;
}

/* 错误提示 */
.bg-red-50 {
  background-color: #fee2e2;
  color: #b91c1c;
  border-left: 4px solid #f87171;
  padding: 0.75rem;
  border-radius: 0.375rem;
  margin-top: 1rem;
  font-size: 0.875rem;
}
</style>
